/**
 * @Author: XiongMeng
 * @Date: 2019-11-11 16:22:53
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2019-12-10 20:20:05
 **/

// 合并请求（key相同的合并，只返回相同项）
export default async (fetchapis = [], mergeKey = "", params = {}, callback) => {
    let result = [], i = 0, x, f, y, res;
    Object.prototype.toString.call(params) === "[object Function]" && (callback = params);
    for (; (f = fetchapis[i]) != null; i++) {
        try {
            res = await f(params);
            if (res.resultCode === 200) {
                if (Object.prototype.toString.call(res.data) === "[object Array]") {
                    res = res.data;
                } else if (Object.prototype.toString.call(res.data.rows) === "[object Array]") {
                    res = res.data.rows;
                }
                if (i) {
                    for (let n = 0; (x = result[n]) != null; n++) {
                        let j = 0;
                        for (; (y = res[j]) != null; j++) {
                            if (x[mergeKey] === y[mergeKey]) {
                                x = { ...x, ...y };
                                res.splice(i, 1);
                                break;
                            }
                        }
                        j > res.length && result.splice(n, 1) && --n;
                    }
                } else {
                    result.push([...res]);
                }
            } else {
                throw (f.name + '接口异常！');
            }
        } catch (e) {
            throw ('接口请求异常：' + e);
        }

    }
    callback && callback(result);
    return result;
}

// 合并请求（key相同的合并，返回所有项）
export const ObjectAssign = async (fetchapis = [], mergeKey = "", params = {}, callback) => {
    let result = [], i = 0, x, f, y, res;
    Object.prototype.toString.call(params) === "[object Function]" && (callback = params);
    for (; (f = fetchapis[i]) != null; i++) {
        try {
            res = await f(params);
            if (res.resultCode === 200) {
                if (Object.prototype.toString.call(res.data) === "[object Array]") {
                    res = res.data;
                } else if (Object.prototype.toString.call(res.data.rows) === "[object Array]") {
                    res = res.data.rows;
                }

                if (i) {
                    for (let j = 0; (y = res[j]) != null; j++) {
                        let n = 0;
                        for (; (x = result[n]) != null; n++) {
                            if (x[mergeKey] === y[mergeKey]) {
                                result[n] = { ...x, ...y };
                                res.splice(j, 1);
                                break;
                            }
                        }
                        n === result.length && result.push(y);
                    }
                } else {
                    result.push(...res);
                }
            } else {
                throw (f.name + '接口异常！');
            }
        } catch (e) {
            throw ('接口请求异常：' + e);
        }

    }

    callback && callback(result);
    return result;
}

// 数组内对象排序
// export const sortRule = (arr, key) => arr = arr.sort((a, b) => a[key] - b[key]);
export const sortRule = (arr, key) => arr = arr.sort((a, b) => {
  if(a[key]<b[key]){
    return -1;
  }else if(a[key]>b[key]){
    return 1;
  }else{
    return 0;
  }
});
